package com.yty.system.controller;


import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.yty.system.config.Producer;
import com.yty.system.entity.AccountInfo;
import com.yty.system.entity.vo.ProducerVo;
import com.yty.system.service.IAccountInfoService;
import org.apache.rocketmq.client.producer.SendResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.awt.*;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author yegang
 * @since 2022-06-21
 */
@RestController
@RequestMapping("/accountInfo")
public class AccountInfoController {

    @Autowired
    private Producer producer;
    @Autowired
    private IAccountInfoService accountInfoService;

    @CrossOrigin
    @GetMapping(value = "/findById")
    public Object findById(){
        PageHelper.startPage(1, 2);
        List<AccountInfo> list = accountInfoService.list();
        PageInfo<AccountInfo> pageInfo = PageInfo.of(list);

        Map map = new HashMap();
        map.put("total", pageInfo.getTotal());
        map.put("data", pageInfo.getList());
        return map;
    }
    @PostMapping(value = "/insert")
    public String insert(@RequestBody AccountInfo accountInfo){
        accountInfoService.save(accountInfo);
        return "success";
    }

    @GetMapping(value = "/sendMsg")
    public SendResult sendMsg(){
        try {
            // 发送消息体内容
            ProducerVo producerVo = new ProducerVo();
            JSONObject jsonObject = new JSONObject();
            jsonObject.put("username","zhangsan");
            jsonObject.put("sex","男");
            jsonObject.put("realName","张三");
            // 发送主题
            SendResult result = producer.syncSend(producerVo);
            return result;
        }catch (Exception e){
            e.printStackTrace();
            return null;
        }
    }

    @GetMapping(value = "/transfer")
    public String transfer(@RequestParam("accountNo")String accountNo, @RequestParam("amount") Double amount){
        try {
            AccountInfo accountInfo = new AccountInfo();
            accountInfoService.transfer(accountInfo);
            return "转账成功";
        }catch (Exception e){
            e.printStackTrace();
            return "转账失败";
        }
    }
}
